unit td_unitgroupModal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BaseForm, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore,
  dxSkinsDefaultPainters, cxStyles, dxSkinscxPCPainter,
  cxDataStorage, DB, cxDBData, Menus,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  StdCtrls, cxButtons, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGroupBox, MisString, MemDS, DBAccess, Uni, dxSkinsForm, cxNavigator,
  cxCustomData, cxFilter, cxData,
  cxDataControllerConditionalFormattingRulesManagerDialog;

type
  TFrmtd_unitgroupModal_List = class(TfrmBaseForm)
    cxGroupBox1: TcxGroupBox;
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    btnAppend: TcxButton;
    btnEdit: TcxButton;
    btnSure: TcxButton;
    btnCancel: TcxButton;
    Qry_unitgroupMst: TUniQuery;
    DS_unitgroupMst: TDataSource;
    Qry_unitgroupMstCode: TStringField;
    Qry_unitgroupMstName: TStringField;
    Qry_unitgroupMstRemark: TStringField;
    Code: TcxGridDBColumn;
      Name: TcxGridDBColumn;
    Remark: TcxGridDBColumn;
    Qry_unitgroupMstGroupId: TIntegerField;
    procedure FormCreate(Sender: TObject);
    procedure btnAppendClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure btnSureClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure Qry_unitgroupMstNewRecord(DataSet: TDataSet);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FGroupId: integer;
    FSureClose: Boolean;
  end;

var
  Frmtd_unitgroupModal_List: TFrmtd_unitgroupModal_List;

implementation

uses td_unitgroupModal_Edit;

{$R *.dfm}

procedure TFrmtd_unitgroupModal_List.FormCreate(Sender: TObject);
begin
  inherited;
  FSureClose := False;
  with Qry_unitgroupMst do
  begin
    if Active then Active := False;
    Sql.Clear;
    SQL.Text := 'select * from td_unitgroupMst order by groupid';
    Open;
  end;
end;

procedure TFrmtd_unitgroupModal_List.btnAppendClick(Sender: TObject);
begin
  inherited;
  try
    Qry_unitgroupMst.Append;
    Frmtd_UnitGroupModal_Edit := TFrmtd_UnitGroupModal_Edit.Create(nil);
    Frmtd_UnitGroupModal_Edit.MainDataSource.DataSet := Qry_unitgroupMst;
    with frmtd_unitgroupmodal_Edit.Qry_unitgroupDtl do
    begin
      Close;
      Sql.Clear;
      Sql.Text := 'select * from td_unitgroupDtl where 1 = 2 ';
      Open;
      Append;
      Post;
    end;
    Frmtd_UnitGroupModal_Edit.ShowModal;
  finally
    if Frmtd_UnitGroupModal_Edit <> nil then
      FreeAndNil(Frmtd_UnitGroupModal_Edit);
  end;
end;

procedure TFrmtd_unitgroupModal_List.btnEditClick(Sender: TObject);
begin
  inherited;
  if Qry_unitgroupMst.IsEmpty then exit;
  try
    Qry_unitgroupMst.Edit;
    Frmtd_UnitGroupModal_Edit := TFrmtd_UnitGroupModal_Edit.Create(nil);
    Frmtd_UnitGroupModal_Edit.MainDataSource.DataSet := Qry_unitgroupMst;
    with Frmtd_UnitGroupModal_Edit.qry_unitgroupDtl do
    begin
      if Active then Active := False;
      Sql.Clear;
      Sql.Text := 'select * from td_unitgroupDtl '
        + 'where groupid = ''' + Qry_unitgroupMst.FieldbyName('groupid').asstring + ''' order by groupid ';
      open;
    end;
    Frmtd_unitgroupModal_Edit.ShowModal;
  finally
    if Frmtd_UnitGroupModal_Edit <> nil then
      FreeAndNil(Frmtd_UnitGroupModal_Edit);
  end;
end;

procedure TFrmtd_unitgroupModal_List.btnSureClick(Sender: TObject);
begin
  inherited;
  if Qry_unitgroupMst.IsEmpty then exit;

  FGroupId := Qry_unitgroupMst.FieldbyName('GroupId').AsInteger;
  FSureClose := True;
  ModalResult := mrOk;
end;

procedure TFrmtd_unitgroupModal_List.btnCancelClick(Sender: TObject);
begin
  inherited;
  FSureClose := False;
  Close;
end;

procedure TFrmtd_unitgroupModal_List.Qry_unitgroupMstNewRecord(
  DataSet: TDataSet);
begin
  inherited;
  DataSet.fieldbyname('groupid').asinteger := GetNewKeyValue('td_unitgroupMst');
  DataSet.FieldByName('code').AsString := GetNewCodeValue('td_unitgroupmst', 'code', 'groupid');
end;

procedure TFrmtd_unitgroupModal_List.FormShow(Sender: TObject);
begin
  inherited;
  cxGrid1.SetFocus;
end;

end.

